﻿using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Web;
using Jayden.Dll.Web.Styles;
using System.Text;
using System.Globalization;

namespace Jayden.Site.Sterdent.Dll
{
	public class MenuOl : Ol
	{
		public void ProcessRequest(HttpHandlerContext context)
		{
			bool found = false;
			Li first = null;
			foreach (Li li in GetControls<Li>())
			{
				if (first == null)
					first = li;
				foreach (A a in li.GetControls<A>())
				{
					if (context.Context.Request.Url.AbsolutePath == a.HRef)
					{
						li.Classes.Add("selected");
						found = true;
						break;
					}
				}
			}
			if (!found && first != null)
				first.Classes.Add("selected");
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			base.ProcessHtmlRequest(context);
			ProcessRequest(context);
		}

		public override void ProcessCssRequest(HttpHandlerContext context)
		{
			base.ProcessCssRequest(context);
			CssElement elt = null;
			if(context.Context.Session["zoom"] != null)
			{
				if(elt == null)
					elt = new Jayden.Dll.Web.Styles.CssElement(".zoomed");
				//elt.AddProperty("zoom", context.Context.Session["zoom"].ToString() + "%");
				string transform = string.Format("scale({0})", ((int)context.Context.Session["zoom"] * 0.01).ToString(CultureInfo.InvariantCulture));
				string transformOrigin = "0% 0%";
				elt.AddProperty("transform-origin", transformOrigin);
				elt.AddProperty("-o-transform-origin", transformOrigin);
				elt.AddProperty("-ms-transform-origin", transformOrigin);
				elt.AddProperty("-moz-transform-origin", transformOrigin);
				elt.AddProperty("-webkit-transform-origin", transformOrigin);
				elt.AddProperty("transform", transform);
				elt.AddProperty("-o-transform", transform);
				elt.AddProperty("-ms-transform", transform);
				elt.AddProperty("-moz-transform", transform);
				elt.AddProperty("-webkit-transform", transform);
				context.Css.AddElement(elt);
			}
			if (context.Context.Session["width"] != null)
			{
				if (elt == null)
					elt = new Jayden.Dll.Web.Styles.CssElement(".zoomed");
				elt.AddProperty("width", context.Context.Session["width"].ToString() + "px");
			}
		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			base.ProcessJsonRequest(context);

			if (context.Request["zoom"] != null)
				context.Context.Session["zoom"] = int.Parse(context.Request["zoom"]);
			if (context.Request["width"] != null)
				context.Context.Session["width"] = int.Parse(context.Request["width"]);
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			base.ProcessJsRequest(context);
			string zoomSet = context.Context.Session["zoom"] == null ? "100" : context.Context.Session["zoom"].ToString();

			StringBuilder builder = new StringBuilder();

			builder.Append(@"
var zoomPct = 82;
var resetZoom = function() {
	var zoom = parseInt($(window).width() * zoomPct / 1024);
	if(zoom > 100) {
		$('.zoomed')
			//.css('zoom', '1')
			.css('transform', 'scale(1)')
			.css('width', 'auto');
		var defaultWidth = $('.zoomed').width();
		var optimizedWidth = parseInt(defaultWidth * 100 / zoom);
		$('.zoomed')
			//.css('zoom', zoom + '%')
			.css('transform', 'scale(' + (zoom / 100) + ')').css('transform-origin', '0% 0%')
			.css('width', optimizedWidth + 'px');
		$.ajax({
			url: window.location.href + '?format=json',
			data: {
				zoom: zoom,
				width: optimizedWidth
			}
		});
	}
};
$(window).resize(resetZoom);
if(Math.abs(parseInt($(window).width() * zoomPct / 1024) - " + zoomSet + @") > 2)
	resetZoom();
");

			context.Js["zoom"].Builder.Append(builder.ToString());

			if (context.Uri["body"] == "/Controls/HomeDiv.xml")
				context.Js["slideshow"].AppendOnInit(@"
	var $slideshowDiv = $('div.slideshow');
	if($slideshowDiv.length) {
		var $images = $('img.slideshow', $slideshowDiv);
		var timePerPicture = 5000;
		$images.css('opacity', 0);
		$($images[0]).css('opacity', 1);
		var counter = 1;
		var previous = 0;
		setInterval(function() {
			var $prev = $($images[previous]);
			var $cur = $($images[counter]);
			$prev.animate({opacity: 0}, 1000);
			$cur.animate({opacity: 1}, 1000);
			previous = counter;
			counter ++;
			if(counter >= $images.length)
				counter = 0;
		}, timePerPicture);
	}
");
		}
	}
}